package com;

import data.CacheUser;
import data.User;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PassByCache {
	
	public final static String SHARED_PREFS_NAME = "com.passby.cache";
	
	public final static String USERS = "users";
	public final static String USER = "user";
	public final static String PASSWORD = "password";
	public final static String ACCESS_TOKEN = "accessToken";
	public final static String TOKEN_SECRET = "tokenSecret";
	public final static String NEW_MESSAGES_COUNT = "new_messages";
	public final static String COMPOSE_GROUP = "compose_group";
	public final static String REFRESH_RATE = "refreshRate";
	public final static String GEOCHAT_NUMBER = "geochat_number";
	
	private final Context context;
	
	public PassByCache(Context context) {
		this.context = context;
	}
	
	public String getUser() {
		return openRead().getString(USER, null);
	}
	
	public CacheUser[] getUsers() {
		String u = openRead().getString(USERS, null);
		if(u == null)
			return null;
		String[] users = u.split(",");
		int len = users.length;
		CacheUser[] result = new CacheUser[len];
		for(int i = 0;i<len;i++)
		{
			String[] userinfo = users[i].split("|");
			CacheUser user = new CacheUser();
			user.username = userinfo[0];
			user.accessToken = userinfo[1];
			user.tokenSecret = userinfo[2];
			result[i]  = user;
		}
		return result;
	}
	
	public String getPassword() {
		return openRead().getString(PASSWORD, null);
	}
	
	public String getAccessToken()
	{
		return openRead().getString(ACCESS_TOKEN, null);
	}
	
	public String getTokenSecret()
	{
		return openRead().getString(TOKEN_SECRET, null);
	}
	
	public void setUser(User user,String acc,String secret) {
		Editor editor = openWrite();
		editor.putString(USER, user.username);
		editor.putString(ACCESS_TOKEN, acc);
		editor.putString(TOKEN_SECRET, secret);
		String s = user.username+"|"+acc+"|"+secret;
		CacheUser[] users = getUsers();
		boolean hasuser = false;
		if(users!=null)
		{
			for(int i=0;i<users.length;i++)
			{
				if(users[i].username == user.username)
				{
					hasuser = true;
					break;
				}
			}
		}
		if(!hasuser)
		{
			String u = "";
			if(users == null || users.length == 0)
			{
				u = s;
			}else
			{
				u = openRead().getString(USERS, "") + "," + s;
			}
			editor.putString(USERS, u);
		}
		editor.commit();
	}
	
	public String getComposeGroup() {
		return openRead().getString(COMPOSE_GROUP, null);
	}
	
	public void setComposeGroup(String group) {
		Editor editor = openWrite();
		if (group == null) {
			editor.remove(COMPOSE_GROUP);
		} else {
			editor.putString(COMPOSE_GROUP, group);
		}
		editor.commit();
	}
	
	public void clearNewMessagesCount() {
		Editor editor = openWrite();
		editor.remove(NEW_MESSAGES_COUNT);
		editor.commit();
	}
	
	public int getNewMessagesCount() {
		return openRead().getInt(NEW_MESSAGES_COUNT, 0);
	}
	
	public void setNewMessagesCount(int count) {
		Editor editor = openWrite();
		editor.putInt(NEW_MESSAGES_COUNT, count);
		editor.commit();
	}
	
	public String getGeoChatNumber() {
		return openRead().getString(GEOCHAT_NUMBER, null);
	}
	
	public int getRefreshRateInMinutes() {
		return Integer.parseInt(openRead().getString(REFRESH_RATE, "15"));
	}
	
	public int getRefreshRateInMilliseconds() {
		return 1000 * 60 * getRefreshRateInMinutes();
	}
	
	private SharedPreferences openRead() {
		return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
	}
	
	private Editor openWrite() {
		return openRead().edit();
	}

	public void clearUserData() {
		Editor editor = openRead().edit();
		editor.remove(NEW_MESSAGES_COUNT);
		editor.remove(COMPOSE_GROUP);
		editor.commit();
	}

}
